package com.company.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by ruoan on 2020/9/25 15:05
 * 代理类
 * 实现代理的步骤：
 * 1. 切面类AnotherUserProxy和被代理类都注册为bean
 * 2. 通过xml或者注解扫描基础包，发现@Aspect注解，并为其所在的类生成代理对象
 * 3. 在通知的前面用注解指示通知的形式。前置通知@Before、后置通知@AfterReturing、异常通知@AfterThrowing
 *    最终通知 @After、 环绕通知 @Around
 *
 */
@Component
@Aspect
@Order(3)
public class AnotherUserProxy {
    // 对AnotherUser类的saySomething方法进行前置通知
    @Before(value = "execution(* com.company.aspectj.AnotherUser.saySomething(..))")
    public void before(){
        System.out.println("AnotherUserProxy--->before");

    }
    //对AnotherUser类的saySomething方法进行最终通知
    @After(value="execution(* com.company.aspectj.AnotherUser.saySomething(..))")
    public void after(){
        System.out.println("AnotherUserProxy--->after");
    }
    //对AnotherUser的saySomething方法进行后置异常通知
    @AfterThrowing(value = "execution(* com.company.aspectj.AnotherUser.saySomething(..))")
    public void afterThrowing(){
        System.out.println("AnotherUserProxy--->afterThrowing");
    }

    //对AnotherUser的saySomething方法进行后置通知
    @AfterReturning(value = "execution(* com.company.aspectj.AnotherUser.saySomething(..))")
    public void afterReturning(){
        System.out.println("AnotherUserProxy--->afterReturning");
    }

    //对AnotherUser的saySomething方法进行环绕通知
    @Around(value = "execution(* com.company.aspectj.AnotherUser.saySomething(..))")
    public void around(ProceedingJoinPoint joinPoint){
        System.out.println("AnotherUserProxy--->around前");
        try {
            joinPoint.proceed(); //执行被代理的方法
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("AnotherUserProxy--->around后");


    }

}
